using System;
using System.Collections.Generic;

using Csla;

using EntModApp.EntServices.Business;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  public partial class CslaPersonInfo : 
    CslaUserInfo, 
    IPerson
  {
    #region [ Business Properties ]

    public static PropertyInfo<string> FirstNamePropertyInfo = RegisterProperty(new PropertyInfo<string>("FirstName"));
    public string FirstName 
    { 
      get
      {
        return ReadProperty(FirstNamePropertyInfo);
      }
      private set
      {
        LoadProperty(FirstNamePropertyInfo, value);
      }
    }

    public static PropertyInfo<string> LastNamePropertyInfo = RegisterProperty(new PropertyInfo<string>("LastName"));
    public string LastName 
    { 
      get
      {
        return ReadProperty(LastNamePropertyInfo);
      }
      private set
      {
        LoadProperty(LastNamePropertyInfo, value);
      }
    }

    public static PropertyInfo<string> EmailPropertyInfo = RegisterProperty(new PropertyInfo<string>("Email"));
    public string Email 
    { 
      get
      {
        return ReadProperty(EmailPropertyInfo);
      }
      private set
      {
        LoadProperty(EmailPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Factory Methods ]
    protected internal static CslaPersonInfo GetPersonInfo(Person data, PersonLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaPersonInfo result = new CslaPersonInfo();
      result.FetchPerson(data, loadSettings);
      return result;
    }

    private static int ForceInitField = 0;
    protected CslaPersonInfo() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]
    protected void FetchPerson(Person data, PersonLoadSettings loadSettings)
    {
      FetchPersonData(data, loadSettings);
      FetchPersonChildren(data, loadSettings);
    }

    protected void FetchPersonData(Person data, PersonLoadSettings loadSettings)
    {
      base.FetchUserData(data, loadSettings);
      this.FirstName = data.FirstName;
      this.LastName = data.LastName;
      this.Email = data.Email;
    }

    protected void FetchPersonChildren(Person data, PersonLoadSettings loadSettings)
    {
      base.FetchUserData(data, loadSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IPerson implementation ]

    string IPerson.FirstName
    {
      get { return this.FirstName; }
      set
      {
        throw new NotSupportedException();
      }
    }

    string IPerson.LastName
    {
      get { return this.LastName; }
      set
      {
        throw new NotSupportedException();
      }
    }

    string IPerson.Email
    {
      get { return this.Email; }
      set
      {
        throw new NotSupportedException();
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return true; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return false; }
    }

    #endregion [ Explicit IPerson implementation ]
  }
}
